package com.xdbigdata.sync.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xdbigdata.sync.entity.*;
import com.xdbigdata.sync.enums.DataTypeEnum;
import com.xdbigdata.sync.model.dto.CommonResult;
import com.xdbigdata.sync.service.*;
import com.xdbigdata.sync.utils.RPCServiceClientUtil;
import com.xdbigdata.sync.utils.XmltoJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author XD
 * @date 2019/9/19 14:28
 */
@Service
@Slf4j
public class TestServiceImpl implements TestService {
    @Autowired
    private JiekouService jiekouService;
    @Autowired
    private BjsjService bjsjService;
    @Autowired
    private StudentbaseService studentbaseService;
    @Autowired
    private TeacherbaseService teacherbaseService;
    @Autowired
    private XslysjService xslysjService;
    @Autowired
    private XszxjbxxService xszxjbxxService;
    @Autowired
    private XscjService xscjService;
    @Autowired
    private YxsdwjbsjService yxsdwjbsjService;
    @Autowired
    private RPCServiceClientUtil rpcServiceClientUtil;

    @Override
    public void saveData() {
        List<Jiekou> jiekouList = jiekouService.list(new QueryWrapper<Jiekou>().eq("success", 1));
        bjsjService.deleteAll();
        for (Jiekou jiekou : jiekouList) {
            try {
                for(int i=1; i<=jiekou.getTotalPage();i++){
                    String result = rpcServiceClientUtil.getStudData(jiekou,i);
                    JSONObject jsonObject = XmltoJsonUtil.xmltoJson(result);
                    if (jiekou.getDataType().equals(DataTypeEnum.xscj.getValue())) {
                        CommonResult<Xscj> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Xscj> xscjs = commonResult.getRecord().getRow();
                        xscjService.saveBatch(xscjs);
                    } else if (jiekou.getDataType().equals(DataTypeEnum.studentbase.getValue())) {
                        CommonResult<Studentbase> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Studentbase> studentbases = commonResult.getRecord().getRow();
                        studentbaseService.saveBatch(studentbases);
                    }else if (jiekou.getDataType().equals(DataTypeEnum.xszxjbxx.getValue())) {
                        CommonResult<Xszxjbxx> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Xszxjbxx> xszxjbxxes = commonResult.getRecord().getRow();
                        xszxjbxxService.saveBatch(xszxjbxxes);
                    }else if (jiekou.getDataType().equals(DataTypeEnum.xslysj.getValue())) {
                        CommonResult<Xslysj> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Xslysj> xslysjs = commonResult.getRecord().getRow();
                        xslysjService.saveBatch(xslysjs);
                    }else if (jiekou.getDataType().equals(DataTypeEnum.teacherbase.getValue())) {
                        CommonResult<Teacherbase> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Teacherbase> teacherbases = commonResult.getRecord().getRow();
                        teacherbaseService.saveBatch(teacherbases);
                    }else if (jiekou.getDataType().equals(DataTypeEnum.bjsj.getValue())) {
                        CommonResult<Bjsj> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Bjsj> bjsjs = commonResult.getRecord().getRow();
                        bjsjService.saveBatch(bjsjs);
                    }else if (jiekou.getDataType().equals(DataTypeEnum.yxsdwjbsj.getValue())) {
                        CommonResult<Yxsdwjbsj> commonResult = JSONObject.toJavaObject(jsonObject, CommonResult.class);
                        List<Yxsdwjbsj> yxsdwjbsjs = commonResult.getRecord().getRow();
                        yxsdwjbsjService.saveBatch(yxsdwjbsjs);
                    }
                }
            } catch (Exception e) {
            }
        }
    }


}
